package cn.zyy.hotel.service.impl;

import cn.zyy.hotel.dao.EmployeeMapper;
import cn.zyy.hotel.entity.Employee;
import cn.zyy.hotel.service.EmployeeService;
import cn.zyy.hotel.utils.PasswordUtil;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.utils.UUIDUtils;
import cn.zyy.hotel.vo.EmployeeVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private RedisTemplate redisTemplate;

    //员工登录
    public Employee login(String loginName, String loginPwd) {
        String key = SystemConstant.LOGINUSER+loginName;
        Employee employee = null;
        if (redisTemplate.hasKey(key)){
            employee = (Employee) redisTemplate.opsForValue().get(key);
        }else {
            //调用根据账号查询员工信息的方法
            employee = employeeMapper.selectEmployeeByLoginName(loginName);
        }
        //判断对象是否为空
        if (employee != null){
            //将密码加密处理
            String newPassword = PasswordUtil.md5(loginPwd, employee.getSalt(), SystemConstant.PASSWORD_COUNT);
            if (employee.getLoginPwd().equals(newPassword)) {
                redisTemplate.opsForValue().set(key,employee);
                return employee;//登录成功
            }
        }
        //登录失败
        return null;
    }
    //员工登录
    public Employee login(String loginName) {
        return employeeMapper.selectEmployeeByLoginName(loginName);
    }

    //根据部门编号查询员工数量
    public int getEmployeeCountByDeptId(Integer deptId) {
        return employeeMapper.getEmployeeCountByDeptId(deptId);
    }

    //根据角色编号查询员工数量
    public int getEmployeeCountByRoleId(Integer roleId) {
        return employeeMapper.getEmployeeCountByRoleId(roleId);
    }

    //查询员工列表
    public List<Employee> selectEmployeeList(EmployeeVO employeeVo) {
        return employeeMapper.selectEmployeeList(employeeVo);
    }

    //添加员工
    public int addEmployee(Employee employee) {
        //加密盐值
        employee.setSalt(UUIDUtils.randomUUID());
        //创建时间
        employee.setCreateDate(new Date());
        //密码
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,employee.getSalt(), SystemConstant.PASSWORD_COUNT));
        return employeeMapper.addEmployee(employee);
    }

    //修改员工
    public int updateEmployee(Employee employee) {
        //设置修改时间
        employee.setModifyDate(new Date());
        return employeeMapper.updateEmployee(employee);
    }

    //删除员工
    public int deleteById(Integer id) {
        //删除员工角色关系表的数据
        employeeMapper.deleteEmployeeAndRole(id);
        //调用删除员工的方法
        return employeeMapper.deleteById(id);
    }

    //重置密码
    public int resetPwd(Integer id) {
        Employee employee = new Employee();
        //必须先设置盐值，再给密码重新加密赋值
        employee.setSalt(UUIDUtils.randomUUID());
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,employee.getSalt(), SystemConstant.PASSWORD_COUNT));
        //员工编号
        employee.setId(id);
        return employeeMapper.updateEmployee(employee);
    }

    //保存员工角色信息
    public boolean saveEmployeeRole(String roleIds, Integer empId) {
        try {
            //先删除员工角色关系表的数据
            employeeMapper.deleteEmployeeAndRole(empId);
            //再保存员工角色关系
            String [] idStr = roleIds.split(",");
            for (int i = 0; i < idStr.length; i++) {
                employeeMapper.addEmployeeRole(idStr[i],empId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //修改密码
    @Override
    public int updatePwd(EmployeeVO employeeVO) {
        Employee employee = new Employee();
        //必须先设置盐值，再给密码重新加密赋值
        employee.setSalt(UUIDUtils.randomUUID());
        employee.setLoginPwd(PasswordUtil.md5(employeeVO.getNewPassword(),employee.getSalt(), SystemConstant.PASSWORD_COUNT));
        //员工编号
        employee.setId(employeeVO.getId());
        return employeeMapper.updateEmployee(employee);
    }

    //根据id查询员工
    @Override
    public Employee selectEmployeeById(Integer id) {
        return employeeMapper.selectEmployeeById(id);
    }


}
